package com.ljb.calendar.week;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ljb.calendar.DateViewAdapter;
import com.ljb.calendar.OnDateViewClickListener;
import com.ljb.calendar.bean.DateBean;

import org.joda.time.LocalDate;

/**
 * Created by ljb on 2018/3/21.
 */

public class WeekView extends LinearLayout {
    private DateViewAdapter mDateViewAdapter;
    private OnDateViewClickListener mOnDateViewClickListener;
    private DateBean mStartDateBean;

    public WeekView(Context context) {
        this(context, null);
    }

    public WeekView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeekView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(true);
        setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setOrientation(HORIZONTAL);
    }

    public void setDateViewAdapter(DateViewAdapter dateViewAdapter) {
        mDateViewAdapter = dateViewAdapter;
    }

    public void setOnDateViewClickListener(OnDateViewClickListener onDateViewClickListener) {
        mOnDateViewClickListener = onDateViewClickListener;
    }

    public DateBean getStartDateBean() {
        return mStartDateBean;
    }

    /**
     * 设置每一页的日期数据
     *
     * @param todayDate
     * @param startDate
     * @param position
     */
    public void setData(LocalDate todayDate, LocalDate startDate, int position) {
        if (mDateViewAdapter == null)
            return;
        for (int i = 0; i < 7; i++) {
            DateBean bean = new DateBean();
            LocalDate plusDays = startDate.plusDays(i);
            bean.setLocalDate(plusDays);
            bean.setTodayDate(todayDate);
            bean.setPositionOfPager(position);
            bean.setMonthFlag(-2);

            if (i == 0) {
                mStartDateBean = bean;
            }

            //添加自定义的日期控件
            View dateView = mDateViewAdapter.getDateView(this, bean);
            LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
            addView(dateView, params);

            //添加点击事件
            setItemClick(dateView, bean);
        }
    }

    private void setItemClick(View dateView, final DateBean bean) {
        if (mOnDateViewClickListener != null)
            dateView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnDateViewClickListener.onDateViewClick(v, bean);
                }
            });
    }
}













